//
//  AddressCell.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/27.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "AddressCell.h"

@interface AddressCell()<UIGestureRecognizerDelegate,UITextFieldDelegate>
/// 0 姓名    1 电话   2 地区   3 详细地址   4 设为默认收货地址
@property (nonatomic,assign) NSInteger sourceType;

@property (nonatomic,strong) UILabel *leftLB;

@property (nonatomic,strong) UITextField *input_TF;

@property (nonatomic,strong) UIButton *toDetailBtn;

@property (nonatomic,strong) UISwitch *setDefaultSwitch;

@property (nonatomic,strong) UIView *lineV;

@end

@implementation AddressCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withSourceType:(NSInteger)sourceType{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.sourceType = sourceType;
        [self initUI];
    }
    return self;
}

#pragma mark - setter
- (void)setModel:(AddressModel *)model{
    _model = model;
    switch (self.sourceType) {
        case 0:
            self.input_TF.text = _model.linker;
            break;
        case 1:
            self.input_TF.text = _model.linkphone;
            break;
        case 2:
            if ([_model.province isEmpty] == NO && _model.province.length > 0) {
                self.input_TF.text = [NSString stringWithFormat:@"%@ %@ %@",_model.province,_model.city,_model.region];
            }
            break;
        case 3:
            self.input_TF.text = _model.address;
            break;
        case 4:
            self.setDefaultSwitch.on = _model.isdefault;
            break;
        default:
            break;
    }
}

#pragma mark - UI
-(void)initUI{
    [self.contentView addSubview:self.leftLB];
    NSArray *titles = @[@"姓名",@"电话",@"地区",@"详细地址",@"设为默认收货地址"];
    NSArray *placeHolders = @[@"收货人姓名",@"收货人手机号",@"选择省/市/区",@"街道门牌、楼层房间号等信息"];
    self.leftLB.text = titles[self.sourceType];
    if (self.sourceType == 4) {
        UIView *topLineV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 12)];
        topLineV.backgroundColor = MainBackColor;
        [self.contentView addSubview:topLineV];
        
        
        [self.contentView addSubview:self.setDefaultSwitch];
        self.leftLB.top = 38;
        self.leftLB.width = ScreenWidth/2;
    }else{
        [self.contentView addSubview:self.input_TF];
        self.input_TF.placeholder = placeHolders[self.sourceType];
        if (self.sourceType == 1) {
            self.input_TF.keyboardType = UIKeyboardTypeNumberPad;
        }else if (self.sourceType == 2) {
            [self.contentView addSubview:self.toDetailBtn];
        }
        if (self.sourceType != 3) {
            [self.contentView addSubview:self.lineV];
        }
    }
}

#pragma mark - lazy
- (UILabel *)leftLB{
    if (!_leftLB) {
        _leftLB = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 80, 22)];
        _leftLB.textAlignment = NSTextAlignmentLeft;
        _leftLB.font = FontSet(16);
        _leftLB.textColor = MainBlackTextColor;
    }
    return _leftLB;
}
- (UITextField *)input_TF{
    if (!_input_TF) {
        _input_TF = [[UITextField alloc] initWithFrame:CGRectMake(102, self.leftLB.top-4, ScreenWidth-102-44, 30)];
        _input_TF.textColor = MainBlackTextColor;
        _input_TF.delegate = self;
    }
    return _input_TF;
}

- (UIButton *)toDetailBtn{
    if (!_toDetailBtn) {
        _toDetailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _toDetailBtn.frame = CGRectMake(ScreenWidth - 44, 10, 30, 35);
        [_toDetailBtn setImage:LoadImage(@"个人中心_查看全部") forState:normal];
        [_toDetailBtn setImageEdgeInsets:UIEdgeInsetsMake(12, 23, 12, 0)];
        [_toDetailBtn addTarget:self action:@selector(selectAddressClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _toDetailBtn;
}

- (UISwitch *)setDefaultSwitch{
    if (!_setDefaultSwitch) {
        _setDefaultSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(ScreenWidth-80, 34, 55, 32)];
        _setDefaultSwitch.on = NO;
        [_setDefaultSwitch addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
    }
    return _setDefaultSwitch;
}

- (UIView *)lineV{
    if (!_lineV) {
        _lineV = [[UIView alloc] initWithFrame:CGRectMake(15, 54, ScreenWidth-30, 1)];
        _lineV.backgroundColor = RGBA(0, 0, 0, 0.1f);
    }
    return _lineV;
}

#pragma mark - SEL
//选择省市区
-(void)selectAddressClick{
    if (self.selectAddressBlock) {
        self.selectAddressBlock();
    }
}
//设置默认
-(void)switchChange:(UISwitch *)swi{
    self.model.isdefault = swi.isOn;
    if (self.updateAddressInfoBlock) {
        self.updateAddressInfoBlock(self.model);
    }
}

#pragma mark - UITextfieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (self.sourceType == 2) {
        [self selectAddressClick];
        return NO;
    }
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.sourceType == 0) {
        self.model.linker = textField.text;
    }else if (self.sourceType == 1){
        self.model.linkphone = textField.text;
    }else if (self.sourceType == 3){
        self.model.address = textField.text;
    }
    if (self.updateAddressInfoBlock) {
        self.updateAddressInfoBlock(self.model);
    }
}

@end
